//
//  UILabel+Extension.swift
//  RE_BasePrivateProject
//
//  Created by mac on 2018/10/08.
//  Copyright © 2018年 Regina. All rights reserved.
//

import UIKit

extension UILabel {
//    /// 主标题 Label
//    public convenience init(_ title:String, textColor: UIColor = REHexRGB(0x000000), fontSize: CGFloat = 15, alignment: NSTextAlignment = .left) {
//        self.init(frame: .zero)
//        self.text = title
//        self.textColor = textColor
//        self.textAlignment  = alignment
//        self.font = REFontMedium(fontSize)
//    }
    
//    /// 主标题 Label
//    public convenience init(_ title:String, textColor: UIColor = REHexRGB(0x000000), font: UIFont =   (15), alignment: NSTextAlignment = .left) {
//        self.init(frame: .zero)
//        self.text = title
//        self.textColor = textColor
//        self.textAlignment  = alignment
//        self.font = font
//    }
    
    // 内容 Label
    public convenience init(content:String, textColor: UIColor = REHexRGB(0x888888), fontSize: CGFloat = 14, alignment: NSTextAlignment = .left) {
        self.init(frame: .zero)
        self.text = content
        self.textColor = textColor
        self.textAlignment  = alignment
        self.font = REFontRegular(fontSize)
    }

    //副标题 Label
    public convenience init(detail:String, textColor: UIColor = REHexRGB(0x000000), fontSize: CGFloat = 12, alignment: NSTextAlignment = .left) {
        self.init(frame: .zero)
        self.text = detail
        self.textColor = textColor
        self.textAlignment  = alignment
        self.font = REFontRegular(fontSize)
    }
    
    //以 : 分割 
    public convenience init(attributeString:String) {
        self.init(frame: .zero)
        let arr = attributeString.components(separatedBy: CharacterSet.init(charactersIn: "："))
        let firstStr = arr.first! + "："
        let secondStr = arr.last!
        let attributeStr = NSMutableAttributedString(string: attributeString)
        attributeStr.addAttributes([.font: REFontRegular(15),.foregroundColor: REHexRGB(0xa3a5a8)], range: NSRange.init(location: 0, length: firstStr.count))
        attributeStr.addAttributes([.font: REFontRegular(15),.foregroundColor: REHexRGB(0x474747)], range: NSRange.init(location: firstStr.count, length: secondStr.count))
        self.attributedText = attributeStr
    }
    
    public func addLongGestureCopy() {
        self.isUserInteractionEnabled = true
        let longPressGesture = UILongPressGestureRecognizer.init(target: self, action: #selector(self.longPressGestureAction(_:)))
        self.addGestureRecognizer(longPressGesture)
    }
    
    @objc private func longPressGestureAction(_ sender: UILongPressGestureRecognizer) {
        
        let pboard : UIPasteboard = UIPasteboard.general
        pboard.string = self.text;
        
        GlobalMessage.showInfo(globalLanguage("InviteFriends_CopySuccess"))
        
//        self.becomeFirstResponder()
//        let copyItem = UIMenuItem(title: "复制", action: #selector(self.copyText(_:)))
//        let menu = UIMenuController.shared
//        menu.menuItems = [copyItem]
//        if menu.isMenuVisible {
//            return
//        }
//        menu.setTargetRect(self.frame, in: self)
//        menu.setMenuVisible(true, animated: true)
    }
    
    @objc private func copyText(_ sender: UIMenuItem) {
        let pboard : UIPasteboard = UIPasteboard.general
        pboard.string = self.text;
    }

}
